package servlet;
import  java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import javax.servlet.*;
import  javax.servlet.http.*;
import javax.servlet.annotation.WebServlet;
import entity.Goods;

@WebServlet("/selectGoodsServlet")
public class SelectGoodsServlet extends HttpServlet{
    private static final long serialVersionUID=1L;
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Connection con1 = null;
        Statement st = null;
        ResultSet rs = null;
        ArrayList<Goods> allGoods = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        try {
            con1 = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?characterEncoding=utf-8", "root", "mrt286933605");
            st = con1.createStatement();
            request.setCharacterEncoding("utf-8");
            rs = st.executeQuery("select * from goods where gname='"+request.getParameter("name")+"' and gprice>"+request.getParameter("price"));
            allGoods = new ArrayList<Goods>();
            while (rs.next()) {
                Goods g = new Goods();
                g.setId(rs.getInt(1));
                g.setGname(rs.getString(2));
                g.setGprice(rs.getDouble(3));
                allGoods.add(g);
            }
            con1.close();
            st.close();
            rs.close();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        request.setAttribute("allGoods", allGoods);
        RequestDispatcher dis = request.getRequestDispatcher("showGoods.jsp");
        dis.forward(request, response);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
